package com.myspace.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.myspace.sdk.MSRequest;
import com.myspace.sdk.MSSDK;
import com.myspace.sdk.MSRequest.MSRequestCallback;
import com.myspace.util.MSDrawableManager;

public class MSFriendsActivity extends MSBaseActivity {

	@Override
    protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setProgressBarIndeterminateVisibility(false);
		
		super.onCreate(savedInstanceState);
        super.setPageName(R.string.PAGE_TITLE_FRIENDS);
		super.setMainView(R.layout.friends);

		getFriends();
	}
	
	/**
	 * Get second page of user's friends. Items per page=20.
	 * startIndex = 21 returns friends 21-40.
	 * 
	 */
	private void getFriends() {
		Map<String, String> queryParams = new HashMap<String, String>();
		queryParams.put("startIndex","21");
		MSSDK.getFriendsWithParameters(queryParams, new GetFriends_MSRequestCallback());
		
		setProgressBarIndeterminateVisibility(true);	
	}
	
	private class GetFriends_MSRequestCallback extends MSRequestCallback {

		public void requestDidFail(MSRequest request, Throwable error) {
			setProgressBarIndeterminateVisibility(false);
			showToast(error.getMessage());
		}

		public void requestDidLoad(MSRequest request, Object result) {
			setProgressBarIndeterminateVisibility(false);
			
			Map<?,?> data = (Map<?,?>)result;
			result  = data.get("data");
			
			if(result instanceof List<?>) {
				List<FriendItem> items = new ArrayList<FriendItem>();
				List<?> friendList = (List<?>) result;
				for (int i=0; i < friendList.size(); i++) {
					Map<?,?> friendObject = (Map<?,?>) friendList.get(i);
					String userName = (String)friendObject.get("userName");
					String userProfileURL = (String)friendObject.get("userProfileURL");
					String userThumbnailImageURL = (String)friendObject.get("userThumbnailImageURL");
					
					FriendItem item = new FriendItem(userName, userProfileURL, userThumbnailImageURL);
					items.add(item);
				}	
				updateView(items);
			}
		}
	}
	
	private void updateView(List<FriendItem> items) {
		GridView gridView = (GridView) findViewById(R.id.friends_tab_view);
		gridView.setAdapter(new GridViewAdapter(this, items));
	}
	
	private class GridViewAdapter extends BaseAdapter {
		private Context mContext;
		private List<FriendItem> mItems;

		public GridViewAdapter(Context c, List<FriendItem> items) {
			mContext = c;
			mItems = items;
		}

		public int getCount() {
			return mItems.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv;
			if (convertView == null) {
				iv = new ImageView(mContext);
				iv.setLayoutParams(new GridView.LayoutParams(85, 85));
				iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
				iv.setPadding(8, 8, 8, 8);
			} else {
				iv = (ImageView) convertView;
			}
			
			FriendItem item = mItems.get(position);
			MSDrawableManager.getInstance().fetchDrawableOnThread(item.getUserThumbnailURL(), iv);
			
			return iv;
		}
	}
	
	private class FriendItem {
		private String mUserThumbnailURL;
		
 		public FriendItem(String userName, String userProfileURL, String userThumbnailURL) {
			mUserThumbnailURL = userThumbnailURL;
		}

		public String getUserThumbnailURL() {
			return mUserThumbnailURL;
		}
	}
}
